package cn.fl.audit.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.audit.condition.AudInterviewInfoQueryCondition;
import cn.fl.audit.condition.AudReturnVisitQueryCondition;
import cn.fl.audit.dto.AudInterviewInfoDTO;
import cn.fl.audit.dto.AudReturnVisitDTO;
import cn.fl.audit.service.IAudInterviewInfoService;
import cn.fl.audit.service.IAuditApiService;
import cn.fl.audit.transform.AudInterviewInfoVOTransform;
import cn.fl.audit.transform.AudReturnVisitVOTransform;
import cn.fl.audit.vo.AudInterviewInfoVO;
import cn.fl.audit.vo.AudReturnVisitVO;
import cn.fl.audit.vo.AudVisitTkVO;
import cn.fl.customer.service.ICstBscInfoService;
import cn.fl.customer.vo.CstBscInfoVO;
import cn.fl.customer.vo.CstNpBscInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.project.condition.PrjPrdBscInfoQueryCondition;
import cn.fl.project.service.IPrjBscInfoService;
import cn.fl.project.service.IPrjPrdBscInfoService;
import cn.fl.project.vo.PrjBscInfoVO;
import cn.fl.project.vo.PrjPrdBscInfoVO;



/**
 * @Description: 面谈信息表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class AudInterviewInfoServiceImpl implements IAudInterviewInfoService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(AudInterviewInfoServiceImpl.class);
	
	@Autowired
	private IAuditApiService auditApiService;
	
	@Autowired
	private IPrjBscInfoService prjBscInfoService;
	
	@Autowired
	private ICstBscInfoService cstBscInfoService;
	
	@Autowired
	private IPrjPrdBscInfoService prjPrdBscInfoService;
	
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;
	

	@Override
	public AudInterviewInfoVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//TODO 业务逻辑
		AudInterviewInfoDTO dto = null;//this.自定义ApiService.selectById(id);
		return AudInterviewInfoVOTransform.toVO(dto);
	}

	@Override
	public Long add(AudInterviewInfoVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		return this.auditApiService.addAudInterviewInfo(AudInterviewInfoVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.audInterviewInfoApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(AudInterviewInfoVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.auditApiService.modifyAudInterviewInfo(AudInterviewInfoVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		//return this.audInterviewInfoApiService.searchListPage(page);
		//TODO 业务逻辑
		return null;
	}

	@Override
	public AudInterviewInfoVO selectByPrjId(Long prjId) throws BaseException {
		if(null==prjId){
			BaseLogUtils.info(logger, "selectByPrjId", "查询对象项目ID不能为空", CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		AudInterviewInfoVO audInterviewInfoVO = new AudInterviewInfoVO();
		AudInterviewInfoQueryCondition qc = new AudInterviewInfoQueryCondition();
		qc.setProjectId(prjId);
		List<AudInterviewInfoDTO> audInterviewInfoList = this.auditApiService.searchAudInterviewInfoList(qc);
		if(CollectionUtils.isNotEmpty(audInterviewInfoList)){
			AudInterviewInfoDTO audInterviewInfoDTO = audInterviewInfoList.get(0);
			audInterviewInfoVO = AudInterviewInfoVOTransform.toVO(audInterviewInfoDTO);
		}else{
			AudReturnVisitQueryCondition returnQc = new AudReturnVisitQueryCondition();
			returnQc.setProjectId(prjId);
			List<AudReturnVisitDTO> audReturnVisitList = this.auditApiService.searchAudReturnVisitList(returnQc, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
			if(CollectionUtils.isNotEmpty(audReturnVisitList)){
				AudReturnVisitVO audReturnVisitVO = AudReturnVisitVOTransform.toVO(audReturnVisitList.get(0));//回访信息
				AudVisitTkVO audVisitTkVO = audReturnVisitVO.getAudVisitTkVO();
				audInterviewInfoVO.setOperateTmCd(audVisitTkVO.getBizExp());//运营年限
				audInterviewInfoVO.setSecondPayResCd(audVisitTkVO.getSecondPayResCd());//第二还款来源
				audInterviewInfoVO.setDriverCd(audVisitTkVO.getDriver());//司机情况
				//婚姻状况
				PrjBscInfoVO prjBscInfoVO = this.prjBscInfoService.selectById(prjId);
				if(null!=prjBscInfoVO && StringUtils.isNotEmpty(prjBscInfoVO.getCstId())){
					Long cstId = prjBscInfoVO.getCstId();
					CstBscInfoVO cstBscInfoVO = this.cstBscInfoService.selectDetailById(cstId);
					if(null!=cstBscInfoVO && null!=cstBscInfoVO.getCstNpBscInfoVO()){
						CstNpBscInfoVO cstNpBscInfoVO = cstBscInfoVO.getCstNpBscInfoVO();
						audInterviewInfoVO.setMarrStatusCd(cstNpBscInfoVO.getMarStsCd());
					}
				}
				
				audInterviewInfoVO.setGdsResCd(audVisitTkVO.getGoodsRes());//运力来源
				audInterviewInfoVO.setOperateLine(audVisitTkVO.getRoute());//运营路线
				//车辆品牌
				PrjPrdBscInfoQueryCondition prjPrdBscQC = new PrjPrdBscInfoQueryCondition();
				prjPrdBscQC.setPrjId(prjId);
				List<PrjPrdBscInfoVO> prjPrdBscInfoVOList = this.prjPrdBscInfoService.searchList(prjPrdBscQC);
				if(CollectionUtils.isNotEmpty(prjPrdBscInfoVOList)){
					StringBuffer br = new StringBuffer();
					for (PrjPrdBscInfoVO prjPrdBscInfoVO : prjPrdBscInfoVOList) {
						br.append(prjPrdBscInfoVO.getPrdNm()+":"+prjPrdBscInfoVO.getBrCdNm()+";");
					}
					audInterviewInfoVO.setBr(br.toString());
				}
				
				audInterviewInfoVO.setStlWayCd(audVisitTkVO.getGdsStlWayCd());//结算方式
				audInterviewInfoVO.setMainGdsTypCd(audVisitTkVO.getMainGdsTypCd());//主货物大类
				audInterviewInfoVO.setGdsTypCd(audVisitTkVO.getGoods());//主运输货物
			}
		}
		
		return audInterviewInfoVO;
	}
	
	/**
	 * 根据字典中代码组值获取组员集合
	 * @param grpCd 代码组值
	 * @return 集合
	 */
	private List<KeyValueVO> getKvList(String grpCd){
		List<KeyValueVO> kvList = new ArrayList<>();
		try {
			List<SysDictionaryDTO> sysDictionaryDTOList = this.sysDictionaryApiService.searchGroup(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), grpCd);
			if(CollectionUtils.isNotEmpty(sysDictionaryDTOList)){
				for (SysDictionaryDTO sysDictionaryDTO : sysDictionaryDTOList) {
					KeyValueVO keyValueVO = new KeyValueVO();
					keyValueVO.setKey(sysDictionaryDTO.getCode());
					keyValueVO.setValue(sysDictionaryDTO.getCodeNm());
					kvList.add(keyValueVO);
				}
			}
		} catch (BaseException e) {
			BaseLogUtils.info(logger, "getKvList", "获取字典项组"+grpCd+"失败.ERROR:"+e.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception e) {
			BaseLogUtils.error(logger, "getKvList", "获取字典项组"+grpCd+"失败,系统错误.ERROR:"+e.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return kvList;
	}

	@Override
	public List<KeyValueVO> searchCstMgrLvlCdList() throws BaseException {
		return this.getKvList(DictionaryConstant.CODE_AUD_CST_MGR_LVL_CD);
	}

	@Override
	public List<KeyValueVO> searchAssetStatusCdList() throws BaseException {
		return this.getKvList(DictionaryConstant.CODE_AUD_ASSET_STATUS_CD);
	}

	@Override
	public List<KeyValueVO> searchMarrStatusCdList() throws BaseException {
		return this.getKvList(DictionaryConstant.CODE_GRP_MAR_STS_CD);
	}

	@Override
	public List<KeyValueVO> searchFirstPayResCdList() throws BaseException {
		return this.getKvList(DictionaryConstant.CODE_AUD_DOWN_PAYMENT_CD);
	}

	@Override
	public List<KeyValueVO> searchRiskCtrlLvlCdList() throws BaseException {
		return this.getKvList(DictionaryConstant.CODE_AUD_RISK_CTRL_LVL_CD);
	}

}
